// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package edu.uoc.tfc.service.security;

import java.util.Collection;

import edu.uoc.tfc.dbmodel.security.Operation;
import edu.uoc.tfc.dbmodel.security.Role;
import edu.uoc.tfc.dbmodel.security.User;
import edu.uoc.tfc.dto.PerfilVO;
import edu.uoc.tfc.dto.security.CriteriaUserVO;
import edu.uoc.tfc.dto.security.OperationVO;
import edu.uoc.tfc.dto.security.ResultatCercaUsuariVO;
import edu.uoc.tfc.dto.security.RoleVO;
import edu.uoc.tfc.dto.security.UserVO;

/**
 * @see edu.uoc.tfc.service.security.SecurityService
 */
public class SecurityServiceImpl
    extends edu.uoc.tfc.service.security.SecurityServiceBase
{

    /**
     * @see edu.uoc.tfc.service.security.SecurityService#getUsuariDetall(String)
     */    
	protected UserVO handleGetUsuariDetall(String userId) throws Exception
    {
    	User u = this.getUserDao().findUserByUserId(userId);
    	if(null!=u){
	    	UserVO user = this.getUserDao().toUserVO(u);
	    	user.setRolVO(this.getRoleDao().toRoleVO(u.getRol()));
	    	return user;
    	}else{
    		return null;
    	}
    }




	@Override
	protected ResultatCercaUsuariVO handleCercaUsuaris(CriteriaUserVO criteriaUser)
			throws Exception {
		
		@SuppressWarnings("unchecked")
        Collection<UserVO> usuarios =
        		getUserDao()
					.findUsersByCriteria(
							getUserDao().TRANSFORM_USERVO, criteriaUser);
		
		ResultatCercaUsuariVO resultat = new ResultatCercaUsuariVO();
		
		resultat.setElements(usuarios);
		resultat.setTotalRecords(usuarios.size());		
		return resultat;
	}


	@Override
	protected RoleVO[] handleCarregaRols() throws Exception {
		
		@SuppressWarnings("unchecked")
		Collection<RoleVO> roles = 
				getRoleDao()
					.loadAll(getRoleDao().TRANSFORM_ROLEVO);
		
		RoleVO[] rolesReturn = new RoleVO[roles.size()];
		int i=0;
		for(RoleVO rol:roles){
			rolesReturn[i]=rol;
			i++;
		}
		return rolesReturn;
	}


	@Override
	protected OperationVO[] handleCarregaOperacionsDelRol(String roleId)
			throws Exception {
		
		Role r = this.getRoleDao().
				load(Long.valueOf(roleId));
		
		OperationVO[] operacions = new OperationVO[r.getOperations().size()];
		int i = 0;
		for(Object op:r.getOperations()){
			Operation o = (Operation) op;
			operacions[i]=getOperationDao().toOperationVO(o);
			i++;
		}
		return operacions;
	}


	@Override
	protected void handleAltaUsuari(UserVO usuari, String roleId) throws Exception {
		User u = User.Factory.newInstance(
				usuari.getUserId(), 
				usuari.getUserId(),
				usuari.getAdress(),
				true,
				usuari.getNom(),
				usuari.getPrimerCognom(),
				usuari.getSegonCognom(),
				this.getRoleDao().load(Long.valueOf(roleId)));
		getUserDao().create(u);
	}

	@Override
	protected void handleModificaUsuari(UserVO usuari, String roleId) throws Exception {
		usuari.setActive(usuari.getActive()==null?false:true);
		usuari.setRolVO((RoleVO)this.getRoleDao().load(getRoleDao().TRANSFORM_ROLEVO, Long.valueOf(roleId)));
		getUserDao().update(getUserDao().userVOToEntity(usuari));
	}


	@Override
	protected void handleModificarUsuariPerfil(PerfilVO perfil)
			throws Exception {
		
		User u = this.getUserDao().findUserByUserId(perfil.getUserId());
    	u.setUserId(perfil.getUserId());
    	u.setNom(perfil.getNom());
    	u.setPrimerCognom(perfil.getPrimerCognom());
    	u.setSegonCognom(perfil.getSegonCognom());
    	u.setAdress(perfil.getAdress());
    	u.setPassword(perfil.getPassword());
    	this.getUserDao().update(u);
	}

	@Override
	protected PerfilVO handleGetUsuariPerfil(String userId) throws Exception {
    	User u = this.getUserDao().findUserByUserId(userId);
    	PerfilVO p = new PerfilVO();
    	p.setUserId(u.getUserId());
    	p.setNom(u.getNom());
    	p.setPrimerCognom(u.getPrimerCognom());
    	p.setSegonCognom(u.getSegonCognom());
    	p.setAdress(u.getAdress());
    	p.setPassword(u.getPassword());
    	p.setRePassword(u.getPassword());
    	return p;
	}


}